package org.example.design.strategy.guess;

/**
 * @author huangshen
 * @version 0
 * @description Hand
 * @since 2022/2/7 11:17
 */
public class Hand {
    /**
     * 石头
     */
    public static final int HAND_VALUE_STONE = 0;
    /**
     * 剪刀
     */
    public static final int HAND_VALUE_CUT = 1;
    /**
     * 布
     */
    public static final int HAND_VALUE_CLOTH = 2;


    public static final Hand[] HAND = {
            new Hand(HAND_VALUE_STONE),
            new Hand(HAND_VALUE_CUT),
            new Hand(HAND_VALUE_CLOTH),
    };

    private static final String[] NAME = {
            "石头",
            "剪子",
            "布"
    };

    /**
     * 猜拳中出的手势的值
     */
    private final int handValue;

    public Hand(int handValue) {
        this.handValue = handValue;
    }

    public static Hand getHand(int handValue) {
        return HAND[handValue];
    }

    /**
     * 胜利
     * @param hand 对手
     * @return 胜利为true
     */
    public boolean isStrongerThan(Hand hand) {
        return fight(hand) == 1;
    }

    /**
     * 输
     * @param hand 对手
     * @return 失败为true
     */
    public boolean isWeakerThan(Hand hand) {
        return fight(hand) == -1;
    }

    private int fight(Hand hand) {
        if (this == hand) {
            return 0;
        } else if((this.handValue + 1) % 3 == hand.handValue) {
            return 1;
        } else {
            return -1;
        }
    }

    @Override
    public String toString() {
        return NAME[handValue];
    }

}
